package com.yonginitagmaker.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import com.yonginitagmaker.common.Constants;

import android.content.Intent;
import android.os.Environment;
import android.util.Log;

public class LogUtil
{
	private static String	TAG		= "APPG_";
	private static boolean	ENABLE	= Constants.DEBUG;

	public static void e(String tag, String msg)
	{
		if(ENABLE)
			Log.e(tag, msg);
	}

	public static void e(String tag, int msg)
	{
		if(ENABLE)
			Log.e(tag, msg + "");
	}

	public static void e(String tag, boolean msg)
	{
		if(ENABLE)
			Log.e(tag, msg + "");
	}

	public static void e(String msg)
	{
		e(TAG, "" + msg);
	}

	public static void e(int msg)
	{
		e("" + msg);
	}

	public static void e(boolean msg)
	{
		e("" + msg);
	}

	public static void d(String tag, String msg)
	{
		if(ENABLE)
			Log.d(tag, msg);
	}

	public static void d(String tag, int msg)
	{
		if(ENABLE)
			Log.d(tag, msg + "");
	}

	public static void d(String tag, boolean msg)
	{
		if(ENABLE)
			Log.d(tag, msg + "");
	}

	public static void d(String msg)
	{
		d(TAG, "" + msg);
	}

	public static void d(int msg)
	{
		d("" + msg);
	}

	public static void d(boolean msg)
	{
		d("" + msg);
	}

	public static void i(String tag, String str)
	{
		if(ENABLE)
		{
			Log.i(tag, str);
		}
	}

	public static void i(String msg)
	{
		i(TAG, "" + msg);
	}

	public static void i(int msg)
	{
		i("" + msg);
	}

	public static void i(boolean msg)
	{
		i("" + msg);
	}

	public static void w(Exception e)
	{
		if(ENABLE)
			e.printStackTrace();
	}

	public static void errorTrace(String TAG, int logTypeID, Exception e)
	{
		e.printStackTrace();
	}

	/**
	 * File 에 메세지 저장
	 * 
	 * @param message
	 */
	public static void write(String message)
	{

		try
		{
			String fileMessage = FormatUtil.FormatDateGetString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + "\t" + message + "\n";

			File file = new File(Environment.getExternalStorageDirectory(), String.format("Log%s.txt", FormatUtil.FormatDateGetString(new Date(), "yyyyMMdd")));

			if(!file.exists())
				file.createNewFile();

			FileWriter fw = new FileWriter(file, true);
			BufferedWriter out = new BufferedWriter(fw);
			out.write(fileMessage);
			out.flush();
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{

		}
	}

	public static void intent(String tag, Intent intent)
	{
		d(tag, "Intent ============================================================");
		for (String iterable_element : intent.getExtras().keySet())
		{
			try
			{
				d(tag, iterable_element + "=" + intent.getStringExtra(iterable_element));
			}
			catch (Exception e)
			{
			}
		}
		d(tag, "Intent ============================================================");
	}

	public static void intent(Intent intent)
	{
		intent(TAG, intent);
	}
}
